var RepairStarForm = function () {
  var StarFormModel = function (star) {
    BaseModel.call(this);

    var self = this;
    var fn = {
      initData: function (star) {
        if (star === undefined) {
          self.starType = '';
          self.starPercent = undefined;
          self.status = 1;
          return;
        }

        var fields = ['id', 'starType', 'status'];
        self.setDataFromObj(star, fields);

        var starPercent = star.starPercent;
        starPercent = _.round(starPercent * 100, 0);
        self.starPercent = starPercent;
      }
    };

    this.checkValid = function(){
      var fields = ['starType', 'starPercent'];
      return this.checkRequiredFields(fields, true);
    };

    this.getObject = function(type){
      var postData = {};
      if (type === 'update') {
        postData.id = this.id;
      }

      var fields = ['starType', 'status'];
      this.setPostData(postData, fields, true, true);

      var starPercent = this.starPercent;
      starPercent = _.round(starPercent / 100, 2);
      postData.starPercent = starPercent;
      return postData;
    };

    fn.initData(star);
  };

  var template = /* html */ `
    <div class="repair-star-form" v-loading.fullscreen.lock="loading">
      <el-form label-width="auto">
        <el-form-item label="星级评级">
          <el-select v-if="type==='add'" class="form-input" v-model="starForm.starType">
            <el-option v-for="item in starList" :key="item.value" :value="item.value" :label="item.label"></el-option>
          </el-select>
          <span v-else>{{i18n.getMsg('machineRepair.repairSetting.star.'+starForm.starType)}}</span>
        </el-form-item>
        <el-form-item label="星级占比">
          <el-input-number style="width:200px" v-model="starForm.starPercent" :min="0" :precision="0" controls-position="right">
          </el-input-number>
          <span>%</span>
        </el-form-item>
        <el-form-item label="状态">
          <el-select class="form-input" v-model="starForm.status">
            <el-option v-for="item in statusList" :key="item.value" :value="item.value" :label="item.label"></el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <div style="text-align:center">
        <el-button type="primary" @click="ensure">
          <span v-if="type==='add'">新增</span>
          <span v-else>修改</span>
        </el-button>
      </div>
    </div>
  `;

  return {
    props: {
      star: {
        type: Object,
        default: function () {
          return null;
        }
      },

      type: {
        type: String,
        default: 'add'
      }
    },

    data: function () {
      var starForm;
      if (this.type === 'add') {
        starForm = new StarFormModel();
      } else {
        starForm = new StarFormModel(this.star);
      }
      return {
        loading: false,
        starForm: starForm,
        statusList: i18n.getDropDown('machineRepair.repairSetting.status', [1, 0]),
        starList: i18n.getDropDown('machineRepair.repairSetting.star', ['1', '2', '3', '4', '5'])
      };
    },

    methods:{
      ensure:function(){
        var starForm = this.starForm;
        var code = starForm.checkValid();
        if (code !== '') {
          var msg = i18n.getMsg(`machineRepair.repairSetting.error.${code}`);
          this.$alert(msg, '信息');
          return;
        }

        var postData = starForm.getObject(this.type);
        this.loading = true;
        var self = this;
        ajaxRequest('workOrder/insertOrUpdateWorkOrderStarType',postData).then(function (msg) {
          self.loading = false;
          var msg = self.type === 'add' ? '新增维修星级记录成功' : '修改维修星级记录成功';
          self.$alert(msg, '信息', {
            callback: function () {
              self.$emit('save-success');
            }
          });
        }).catch(function(){
          self.loading = false;
        });
      }
    },

    template: template
  };
}();
